import axios from 'axios';

interface Config {
  id?: number;
  key: string;
  value: string;
  description?: string;
}

export const getConfigs = async (): Promise<Config[]> => {
  try {
    const response = await axios.get('/api/configs');
    if (Array.isArray(response.data)) {
      return response.data;
    }
    throw new Error('API返回数据格式不正确');
  } catch (error) {
    console.error('获取配置列表失败:', error);
    throw error;
  }
};

export const updateConfig = async (config: Config): Promise<void> => {
  try {
    await axios.put(`/api/configs/${config.id}`, config);
  } catch (error) {
    console.error('更新配置失败:', error);
    throw error;
  }
};

export const getConfigByKey = async (key: string): Promise<Config> => {
  try {
    const response = await axios.get(`/api/configs/${key}`);
    return response.data;
  } catch (error) {
    console.error('获取配置失败:', error);
    throw error;
  }
};